<template>
  <a-modal
    title="创建数据"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
    okText="确定"
    cancelText="取消"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
          <k-form-build :value="jsonData" ref="KFB"/>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    item: {
      type: Object
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      dataList: {},
      // 收集数据 
      jsonData: {},
      form: this.$form.createForm(this)
    }
  },
  created () {
    console.log('updata modal created')
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  watch: {
    dataList(value) {
      console.log(value)
      // this.$emit('getdata', value)
    },
    item(value) {
      console.log('------------forms.value-------------', value)
      // 渲染数据

    }
  },
  mounted() {
      this.jsonData = this.item.forms
      console.log('------------jsonData-------------', this.item.forms)
  },
  methods: {
      getData() {
      // 通过函数获取数据
      this.$refs.KFB.getData()
        .then(res => {
          // 获取数据成功
          this.$emit('getdata', res)
        })
        .catch(err => {
          console.log(err, "校验失败");
      });
    }
  }
}
</script>
